package web.pre;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import entity.News;
import entity.Product;
import entity.ProductCategory;
import service.NewsService;
import service.ProductCategoryService;
import service.ProductService;
import utils.PageUtils;
import utils.ReturnResult;

public class productCategoryServlet extends HttpServlet{
	public ProductService  productService=new ProductService();
	public ProductService  pro=new ProductService();
	public NewsService news=new NewsService();
	private ProductCategoryService productCategoryService = new ProductCategoryService();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String action = request.getParameter("action");
		if (action != null && action.length()>0) {
			switch (action) {
			case "index":
				index(request,response);
				break;
			case "toAddProductCategory":
				toAddProductCategory(request,response);
				break;
			case "queryProductCategoryList":
				queryProductCategoryList(request,response);
				break;
			case "addProductCategory":
				addProductCategory(request,response);
				break;
			case "toUpdateProductCategory":
				toUpdateProductCategory(request,response);
				break;
			case "modifyProductCategory":
				modifyProductCategory(request,response);
				break;
			case "deleteProductCategory":
				deleteProductCategory(request,response);
				break;
			default:
				response.sendRedirect("404.jsp");
				break;
			}
		} else {
			response.sendRedirect("404.jsp");
		}
	}

	private void deleteProductCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		int id = Integer.parseInt(request.getParameter("id"));
		int count =productCategoryService.detele(id);
		ReturnResult returnResult = new ReturnResult(0,"删除分类失败");

		if (count > 0) {
			returnResult.setStatus(1);
			returnResult.setMessage("删除分类成功");
		}

		String jsonStr = JSON.toJSONString(returnResult);

		out.print(jsonStr);

		out.flush();
		out.close();
	}

	private void modifyProductCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();

		Integer parentId=0;
		//获取分类级别
		String type=request.getParameter("type");
		String productCategoryLevel1=request.getParameter("productCategoryLevel1");
		String productCategoryLevel2=request.getParameter("productCategoryLevel2");
		String name=request.getParameter("name");
		String id=request.getParameter("id");
		if(type.equals("1")){
			parentId =0;
		}else if(type.equals("2")){
			parentId =Integer.parseInt(productCategoryLevel1);
		}else{
			parentId =Integer.parseInt(productCategoryLevel2);
		}
		ProductCategory productCategory =new ProductCategory();
		productCategory.setId(Integer.parseInt(id));
		productCategory.setName(name);
		productCategory.setParentId(parentId);
		productCategory.setType(Integer.parseInt(type));
		productCategory.setIconClass("");
		int result = productCategoryService.update(productCategory);

		ReturnResult returnResult = new ReturnResult(0,"修改分类失败");

		if (result > 0) {
			returnResult.setStatus(1);
			returnResult.setMessage("修改分类成功");
		}

		String jsonStr = JSON.toJSONString(returnResult);

		out.print(jsonStr);

		out.flush();
		out.close();

	}

	private void toUpdateProductCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		ProductCategory productCategory=productCategoryService.findById(Integer.parseInt(id));
		List<ProductCategory> productCategoryList1=null;
		List<ProductCategory> productCategoryList2=null;
		request.setAttribute("productCategory",productCategory);
		//判断分类级别
		if(productCategory.getType()==2){
			productCategoryList1=productCategoryService.findListByType(1);
		}
		if(productCategory.getType()==3){
			//拿到所有的一级分类
			productCategoryList1=productCategoryService.findListByType(1);

			//二级分类要先确定productCategory对应的一级分类
			//拿到当前分类的父级分类
			ProductCategory parentCategory = productCategoryService.findById(productCategory.getParentId());
			//拿到二级分类
			productCategoryList2=productCategoryService.findAllByParentId(parentCategory.getParentId());

			request.setAttribute("parentCategory",parentCategory);
		}
		request.setAttribute("productCategoryList1",productCategoryList1);
		request.setAttribute("productCategoryList2",productCategoryList2);


		request.getRequestDispatcher("/backend/productCategory/toAddProductCategory2.jsp").forward(request, response);

	}

	private void addProductCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();

		Integer parentId=0;
		//获取分类级别
		String type=request.getParameter("type");
		String productCategoryLevel1=request.getParameter("productCategoryLevel1");
		String productCategoryLevel2=request.getParameter("productCategoryLevel2");
		String name=request.getParameter("name");
		if(type.equals("1")){
			parentId =0;
		}else if(type.equals("2")){
			parentId =Integer.parseInt(productCategoryLevel1);
		}else{
			parentId =Integer.parseInt(productCategoryLevel2);
		}
		ProductCategory productCategory =new ProductCategory();
		productCategory.setName(name);
		productCategory.setParentId(parentId);
		productCategory.setType(Integer.parseInt(type));
		productCategory.setIconClass("");
		int result = productCategoryService.add(productCategory);

		ReturnResult returnResult = new ReturnResult(0,"新增分类失败");

		if (result > 0) {
			returnResult.setStatus(1);
			returnResult.setMessage("新增分类成功");
		}

		String jsonStr = JSON.toJSONString(returnResult);

		out.print(jsonStr);

		out.flush();
		out.close();

	}

	private void queryProductCategoryList(HttpServletRequest request, HttpServletResponse response) throws IOException {


		String parentId=request.getParameter("parentId");


		List<ProductCategory> productCategoryList=productCategoryService.findAllByParentId(Integer.parseInt(parentId));

		PrintWriter out = response.getWriter();

		String jsonStr = JSON.toJSONString(productCategoryList);

		out.print(jsonStr);

		out.flush();
		out.close();
	}
	private void toAddProductCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.getRequestDispatcher("/backend/productCategory/toAddProductCategory.jsp").forward(request, response);
	}

	private void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int currPageNo = 1;
		int pageSize = 8;

		String paramCurrPageNo = request.getParameter("currPage");
		if (paramCurrPageNo != null && !"".equals(paramCurrPageNo)) {
			currPageNo = Integer.parseInt(paramCurrPageNo);
		}

		PageUtils<ProductCategory> pageUtil = productCategoryService.findAllProductCategory(currPageNo, pageSize);

		pageUtil.setCurrPage(currPageNo);
		//listProduct
		request.setAttribute("pageUtil", pageUtil);
		request.getRequestDispatcher("/backend/productCategory/productCategoryList.jsp").forward(request, response);


	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
